package ua.kpi_java_training.zastupaylo.module1;

import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

import java.util.ArrayList;


/**
 * Tests for Division.class
 *
 * @version 1.4
 * @author Zastupaylo Alexander (zastupaylo@gmailcom)
 */

public class DivisionTest {

    @Before
    public void setUp() {
        Division.dividendDigits.clear();
    }

    @Test
    public void testIsPositiveSignTrue() {
        String error = "Test IsPositiveSignTrue FAILED, input numbers are positive";
        Division.dividend = 10;
        Division.divisor = 2;
        Division.isPositiveSign();
        assertEquals(error, Division.positiveSign, true);
        error = "Test IsPositiveSignTrue FAILED, input numbers are negative";
        Division.dividend = -10;
        Division.divisor = -2;
        Division.isPositiveSign();
        assertEquals(error, Division.positiveSign, true);
    }

    @Test
    public void testIsPositiveSignFalse() {
        String error = "Test IsPositiveSignFalse FAILED, dividend is negative, divisor - positive";
        Division.dividend = -10;
        Division.divisor = 2;
        Division.isPositiveSign();
        assertEquals(error, Division.positiveSign, false);
        error = "Test IsPositiveSignFalse FAILED, dividend is positive, divisor is negative";
        Division.dividend = 10;
        Division.divisor = -2;
        Division.isPositiveSign();
        assertEquals(error, Division.positiveSign, false);
    }

    @Test
    public void testConvertDividendToDigits() {
        String error = "Test ConvertDividendToDigits FAILED";
        ArrayList<Integer> expectedDigits = new ArrayList<Integer>() {{
            add(1);
            add(2);
            add(3);
            add(4);
        }};
        Division.convertDividendToDigits(1234);
        assertEquals(error, Division.dividendDigits, expectedDigits);
    }

    @Test
    public void testConvertZeroDividendToDigits() {
        String error = "Test ConvertZeroDividendToDigits FAILED";
        ArrayList<Integer> expectedDigits = new ArrayList<Integer>() {{
            add(0);
        }};
        Division.convertDividendToDigits(0);
        assertEquals(error, Division.dividendDigits, expectedDigits);
    }

    @Test
    public void testDivide() {
        String error = "Test Divide FAILED: quotient incorrect.";
        int expectedQuotient = 1;
        Division.divisor = 6;
        int expectedRemainder = 4;
        int actualRemainder = Division.divide(new ArrayList<Integer>(), 10);
        assertEquals(error, Division.quotient, expectedQuotient);
        error = "Test Divide FAILED: remainder incorrect";
        assertEquals(error, actualRemainder, expectedRemainder);
    }

    @Test
    public void testAddDigitToCurrDividend() {
        String error = "Test AddDigitToCurrDividend FAILED";
        int expectedDividend = 13;
        Division.dividendDigits.add(3);
        int currDividend = 1;
        int actualDividend = Division.addDigitToCurrDividend(currDividend);
        assertEquals(error, actualDividend, expectedDividend);
    }

    /**
     * Test for checking adding zero to quotient, because of high order of dividend.
     */
    @Test
    public void testCheckAddingZeroToQuotient() {
        String error = "Test CheckAddingZeroToQuotient FAILED";
        int expectedQuotient = 10;
        Division.quotient = 1;
        Division.divisor = 6;
        int currDividend = 3;
        Division.checkAddingZeroToQuotient(currDividend);
        assertEquals(error, Division.quotient, expectedQuotient);
    }

    /**
     * Don't add zero to quotient, because of high order of dividend, if this is first iteration.
     * Quotient can't begin from zero.
     */
    @Test
    public void testCheckAddingZeroToQuotientZeroCondition() {
        String error = "Test CheckAddingZeroToQuotientZeroCondition FAILED";
        int expectedQuotient = 0;
        Division.quotient = 0;
        Division.divisor = 6;
        int currDividend = 3;
        Division.checkAddingZeroToQuotient(currDividend);
        assertEquals(error, Division.quotient, expectedQuotient);

    }

    /**
     * Don't add zero to quotient, if received currDividend after adding a digit to it is bigger then divisor.
     */
    @Test
    public void testCheckAddingZeroToQuotientDividendCondition() {
        String error = "Test CheckAddingZeroToQuotientDividendCondition FAILED";
        Division.quotient = 1;
        int currDividend = 10;
        int expectedQuotient = 1;
        Division.checkAddingZeroToQuotient(currDividend);
        assertEquals(error, Division.quotient, expectedQuotient);
    }

    @Test
    public void testMakeDivision() {
        String error = "Test MakeDivision FAILED";
        ArrayList<Integer> expected = new ArrayList<Integer>(){{
            add(12);
            add(12);
            add(34);
            add(24);
            add(10);
        }};
        Division.dividendDigits.add(1);
        Division.dividendDigits.add(2);
        Division.dividendDigits.add(3);
        Division.dividendDigits.add(4);
        Division.divisor = 12;
        Division.quotient =  0;
        ArrayList actual = Division.makeDivision();
        assertEquals(error, actual, expected);
    }
}